package org.ku.hero.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.ku.hero.api.protocol.Command;
import org.ku.hero.api.protocol.Packet;
import org.ku.hero.util.PacketUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 心跳 应答
 */
public class HeartBeatRespHandler extends ChannelInboundHandlerAdapter {
    private static final Logger LOGGER = LoggerFactory.getLogger(HeartBeatRespHandler.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        final Packet packet = (Packet)msg;
        if (Command.toCmd(packet.getCmd()) == Command.HEART_BEAT){
            LOGGER.debug("服务端接收到了客户端心跳信息...");
            Packet hb = PacketUtil.heartBeatAck(packet);
            ctx.writeAndFlush(hb);
            return;
        }

        ctx.fireChannelRead(msg);
    }
}
